package com.lingnan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Apply;
import com.lingnan.pojo.JoinedActive;
import com.lingnan.pojo.SelfActive;
import com.lingnan.service.ActiveService;
import com.lingnan.service.ApplyService;
import com.lingnan.service.JoinedActiveService;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.ActiveVo;
import com.lingnan.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 报名表 前端控制器
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@RestController
@CrossOrigin
@RequestMapping("/apply")
public class ApplyController {


    @Autowired
    private ApplyService applyService;

    @Autowired
    private ActiveService activeService;

    @Autowired
    private JoinedActiveService joinedActiveService;

    @PostMapping("/addApplyActive")
    @ApiOperation("报名活动")
    public Result addApplyActive(@ApiParam("传报名对象的no和活动id")@RequestBody ActiveVo activeVo){
        if (!ObjectUtils.isEmpty(activeVo)){
            QueryWrapper<Apply> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("no",activeVo.getNo());
            queryWrapper.eq("active_id",activeVo.getId());
            Apply one = applyService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(one)){
                Apply apply = null;
                apply = applyService.addApplyActive(activeVo);
            if (!ObjectUtils.isEmpty(apply)){
                return ResultGenerator.genSuccessResult(200,apply,"已报名的活动成功！");
            }return ResultGenerator.genErrorResult(201,"已报名的活动失败！");
        }
        return ResultGenerator.genErrorResult(202,"该活动已报名！");
        }
        return ResultGenerator.genErrorResult(201,"已报名的活动失败！输入信息有误");
    }


    @PostMapping("/findApplyActive")
    @ApiOperation("查找报名活动列表")
    public Result findApplyActive(@ApiParam("传用户no")@RequestBody UserVo userVo){
        List<Active> applies = null;
        if (!ObjectUtils.isEmpty(userVo)){
            applies = applyService.findAllByNo(userVo);
        }
        if (!ObjectUtils.isEmpty(applies)){
            return ResultGenerator.genSuccessResult(200,applies,"已报名的活动展示成功！");
        }
        return ResultGenerator.genErrorResult(201,"已报名的活动展示失败！");
    }

    @PostMapping("/findJoinedActive")
    @ApiOperation("查找已参加的活动")
    public Result findJoinedActive(@ApiParam("传用户no")@RequestBody UserVo userVo){

        JoinedActive actives = null;
        if (!ObjectUtils.isEmpty(userVo)){
            actives = joinedActiveService.findAllByNo(userVo);
        }
        if (!ObjectUtils.isEmpty(actives)){
            return ResultGenerator.genSuccessResult(200,actives,"已参加的活动展示成功！");
        }
        return ResultGenerator.genErrorResult(201,"已参加的活动展示失败！");

    }


}

